
package com.insigma.business.qrcode.job.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.insigma.business.common.dto.CurrentUser;
import com.insigma.business.common.utils.CurrentUserUtil;
import com.insigma.business.qrcode.common.converter.TrainingMentorProviderConverter;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.util.RpcContextUtil;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.SessionSoldier;
import com.insigma.business.qrcode.job._bff.TrainingMentorBFFProvider;
import com.insigma.business.qrcode.job.dto.TrainingMentorDTO;
import com.insigma.business.qrcode.job.dto.request.TrainingMentorInsertRequest;
import com.insigma.business.qrcode.job.dto.request.TrainingMentorSearchRequest;
import com.insigma.business.qrcode.job.dto.request.TrainingMentorUpdateRequest;
import com.insigma.business.qrcode.job.dto.search.TrainingMentorBSearcher;
import com.insigma.business.qrcode.job.entity.TrainingMentorDO;
import com.insigma.business.qrcode.job.service.TrainingMentorService;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantEntryApply;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("trainingMentorBFFProvider")
public class TrainingMentorBFFProviderImpl extends BaseProviderImpl implements TrainingMentorBFFProvider {


    @Autowired
    private TrainingMentorService trainingMentorService;


    @Override
    public BasePagingResponse<List<TrainingMentorDTO>> searchTrainingMentor(final TrainingMentorSearchRequest searchRequest) {

        final BasePagingResponse<List<TrainingMentorDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页TrainingMentor的请求参数不能为空");
            }

            @Override
            public void executeService() {
                PageHelper.startPage(searchRequest.getPageNo(), searchRequest.getPageSize());
                final TrainingMentorBSearcher trainingMentorBSearcher = TrainingMentorProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<TrainingMentorDO> trainingMentorDOList = trainingMentorService.search(trainingMentorBSearcher);
                PageInfo<TrainingMentorDO> pageInfo = new PageInfo<>(trainingMentorDOList);
                final List<TrainingMentorDTO> trainingMentorDTOList = TrainingMentorProviderConverter.convertResponseDOList2DTOList(trainingMentorDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, trainingMentorDTOList, searchRequest,pageInfo.getTotal());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.TRAINING_MENTOR_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<TrainingMentorDTO> getTrainingMentor(final BaseGetRequest baseGetRequest){

        final BaseResponse<TrainingMentorDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条TrainingMentor的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final TrainingMentorDO trainingMentorDO = trainingMentorService.get((String)baseGetRequest.getId());
                final TrainingMentorDTO remoteObj = TrainingMentorProviderConverter.convertResponseDO2DTO(trainingMentorDO);
                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.TRAINING_MENTOR_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertTrainingMentor(final TrainingMentorInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增TrainingMentor的请求参数不能为空");
            }
            @Override
            public void executeService() {
                final TrainingMentorDO trainingMentorDO = TrainingMentorProviderConverter.convertRequestIO2DO(insertRequest);
                trainingMentorDO.setPublished(true);
                trainingMentorService.insert(trainingMentorDO);
                ResponseUtils.setupSuccessResponse(baseResponse, trainingMentorDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.TRAINING_MENTOR_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateTrainingMentor(final TrainingMentorUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改TrainingMentor的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");
            }
            @Override
            public void executeService() {
                TrainingMentorDO trainingMentorDO = TrainingMentorProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = trainingMentorService.update(trainingMentorDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.TRAINING_MENTOR_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteTrainingMentor(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除TrainingMentor的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
//                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(),"操作人类型不能为空");
//                BizCheckUtil.checkNotBlank((String)deleteRequest.getModifierId(),"操作人不能为空");
            }
            @Override
            public void executeService() {
                CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                final Integer affectRowNum = trainingMentorService.delete((String)deleteRequest.getId(),currentUser.getUserType(),currentUser.getUserId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.TRAINING_MENTOR_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }
}
